<?php

/**
 * @author     Thibaut Cromphaut, Gaïtano Boeraeve, Matthias Mercy
 * @copyright  Copyright (c) 2011 memorygame
 */
class LevelController extends Zend_Controller_Action
{

    public function init()
    {
        $this->stylesBase = $this->view->baseUrl() . '/styles/';
    }
    
    public function preDispatch()
    {
        // Stuur de ingelogde gebruiker naar de homepagina, tenzij er uitgelogd wordt.
        if ( Zend_Auth::getInstance()->hasIdentity() ) {
            switch ( $this->getRequest()->getActionName() ) {
                case 'index':
                    break;
                 case 'edit':
                    break;
                case 'delete':
                    break;
                default:
                    $this->_helper->redirector('index', 'index');
                    break;
            }
        }    
    }
    
    public function indexAction()
    {   
        $view = $this->_getView('Levels');  
        $levels = $this->_getAllLevels();  
        $view->levels = $levels;
    }
    
    public function editAction()
    {   
        $view = $this->_getView('Edit level');
        $form = $this->_getEditLevelForm();
        
        if ($this->getRequest()->isPost()) 
	{
            $formData  = $this->_request->getPost();
            if ($form->isValid($formData)) 
            {
                $view = $this->_getView('Fout - Toevoegen');  
                
                // Promotion toevoegen 
                $values = $form->getValues();

                $level_values['id'] = $values['level_id'];
                $level_values['name'] = $values['level_name'];
                $level_values['time'] = $values['level_time'];
                $level_values['numberOfCards'] = $values['level_numberofcards'];
                $level_values['pictureUrl'] = $values['level_pictureUrl'];
                $level_values['worldId'] = $values['worlds_world_id'];
       
                $level = new Application_Model_Level($level_values);
                
                //UPLOAD THE PICTURE
                    $level_picture = $form->level_pictureUrl->getFileName();
                    //Zend_Debug::dump($world_picture);
                    //die("upload");
                    if(strlen($level_picture))
                    {
                        $upload_location   = $level_picture;
                        $level_extension   = end(explode('.', $level_picture));
                        //$level_location    = "assets/levels/";
                        $level_location    = "../../CleverSpriteMemoryGame-debug/assets/levels/";
                        $level_savename    = 'world_'.$level->Id. '.' . $level_extension;
                        $level_savepath    = $level_location.$level_savename;

                        // Manually do the uploading and renaming, as the Zend way seems to fail
                        if(file_exists($level_location))
                        {
                            if(rename($upload_location, $level_savepath))
                                $level->PictureUrl   = $level_savename;
                        }
                    }
                
                
                
                $levelMapper = new Application_Model_LevelMapper();
                $levelMapper->save($level);
                    
                return $this->_helper->redirector('index', 'level');                
            }
        }else{
            $id = (string)$this->getRequest()->getParam('id');
            
            $level = $this->_getViewLevel($id);
            
            if(!isset($level->World->Id))
            {
                $worldId = 0;
            }
            else
            {
                $worldId = $level->World->Id;
            }
                    
            
            $form->populate(
                array(
                    'level_id'              => $level->Id,
                    'level_name'            => $level->Name,    
                    'level_time'            => $level->Time,
                    'level_numberofcards'   => $level->NumberOfCards,
                    'level_pictureUrl'      => $level->PictureUrl,                    
                    'worlds_world_id'       => $level->worldId,
                ));
            $view->form = $form; 
        }   
    }
    
    public function deleteAction()
    {
        $id = (string)$this->getRequest()->getParam('id');
        $levelMapper = new Application_Model_LevelMapper();
        $levelMapper->delete($id);
        $this->_helper->redirector('index', 'level');
    }

    /**
     * @param string $title
     * @return Zend_View
     */
    private function _getView($title)
    {
        $view = $this->view;
        $view->title = $title;
        $view->headTitle($view->title, 'prepend');                           
        $view->headLink()->appendStylesheet($this->stylesBase . 'main.css'); 

        return $view;
    }
    
    private function _getEditLevelForm()
    {   
        return new Application_Form_EditLevel(array(
            'action' => $this->view->baseUrl() . '/level/edit/',
            'post'   => 'post',
        ));
    }
    
    /**
     * @return Array
     */
    private function _getAllLevels()
    {
        $levelMapper = new Application_Model_LevelMapper();
        $levelArray = $levelMapper->fetchAll();   
        
        
        /*$userHasLevelMapper = new Application_Model_UserHasLevelMapper();
        $userHasLevelMapper->fetchFirst();*/
        

        return $levelArray;
    }
    
    private function _getViewLevel($id)
    {    
        $levelMapper = new Application_Model_LevelMapper();
        $levelArray = $levelMapper->fetchById($id);

        return $levelArray;
    }
}





